﻿/**
 * 验证:
 * type:验证类型
 * 		1: 长号手机验证(默认11位)
 * 		2：固话号码验证(未完成)
 * 		3：纯数字密码验证(默认6位)
 * 		4：字母数字组合密码验证（默认6位）
 * 		5：只能输入数字或带小数点(小数点后面只能有两位小数)的数字或-1
 * 		6:只能输入19位银行卡
 * 		7:只能输入数字	
 * 		8:地址不能是纯数字
 * 		
 * 
 * 		/^[1-9](\d{0,3})(\.\d+)?$/    //只能输入带小数点的金额
 * str:需要验证的字符串
 * 
 */
function vaild_pass(str_, type, count) {
    var reg_str = null;

    switch (type) {
        case 1:
            reg_str = /^(13|14|15|18|17)[0-9]{9}/;
            break;
        case 2:
            reg_str = /(\(\d{3,4}\)|\d{3,4}-|\s)?\d{8}/;
            break;
        case 3:
            reg_str = /[0-9]{6}/;
            break;
        case 4:
            reg_str = /^[0-9a-zA-Z_#]{6,16}$/;//[0-9a-zA-Z]{6}
            break;
        case 5:
            reg_str = /^(\d+(?:\.\d{2})?|-1)$/;
            break;
        case 6:
            reg_str = /^\d{19}$/;
            break;
        case 7:
            reg_str = /^[0-9]*$/;
            break;
        case 8:
            reg_str = /^[0-9]*$/;
            break;
        case 9:
            reg_str = /^(\d{16}|\d{18}|\d{19})$/;
            break;
    }

    return reg_str.test(str_);
}

/**
 * 判断字符串是否为""，或 判断对象是否为 null
 * */
function isBlank(str) {
    var type = typeof str;
    var result = false;
    switch (type) {
        case "string":
            if (str == "" || isUndefined(str)) {
                result = true;
            }
            break;
        case "objecct":
            if (isUndefined(str) || isNull(str)) {
                result == true;
            }
            break;
    }

    return result;
}

//判断对象是否为 undefined
function isUndefined(obj) {
    return obj == undefined;
}

//判断对象是否为 null
function isNull(obj) {
    return obj == null;
}

///////////////////倒计时  -start /////////////////////////
var timer__ = 0;
/**
 * @param target string 进行倒计时目标元素选择器
 * @param allTime int 倒计时时长
 * @param text string 倒计时结束后目标元素显示的文字
 * @param callback 倒计时结束后回调函数,可有可无
 * */
function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++){
        var c = ca[i];
        while (c.charAt(0)==' '){
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) == 0){
            return unescape(c.substring(nameEQ.length,c.length));
        }
    }
    return false;
}
function clearCookie(name){
    setCookie(name, "", -1);
}
function setCookie(name,value,seconds){
    seconds = seconds || 0;
    var expires = "";
    if (seconds != 0 ){
        var date = new Date();
        date.setTime(date.getTime()+(seconds*1000));
        expires = "; expires="+date.toGMTString();
    }
    document.cookie = name+"="+escape(value)+expires+"; path=/";
}

function timeTick(target,alltick,text,callback){ //倒计时入口
    $(target).css("pointer-events","none");
    
    sessionStorage.tick_text = text;
    sessionStorage.tick_target = target;
    
    var lastTick = sessionStorage.tick_; //剩余的秒数
    if((lastTick && lastTick != "1") || alltick > 1){ //读秒
        var isTicking = getCookie("ticking");
        if(!isTicking){
            setCookie("ticking", 1, alltick);
        }
        sessionStorage.tick_ = -- alltick;
        $(target).html(alltick+"s");
        if(callback) callback();
        timer__ = setTimeout(function(){
            timeTick(target,alltick,text,callback);
        },1000);
    }else{
        //不读秒
        init_tick();
        if(callback) callback();
    }
}


function auto_reflash(){
    var isTicking = getCookie("ticking");
    var lastTick = sessionStorage.tick_;
    var text = sessionStorage.tick_text;
    var target = sessionStorage.tick_target;
    
    if(isTicking && (isTicking == 1 || isTicking == "1")){
        timeTick(target, lastTick, text);
    }else{
        init_tick();
    }
}

function init_tick(){
    var text = sessionStorage.tick_text;
    var target = sessionStorage.tick_target;
    
    if(target){
        $(target).removeAttr("style");
        //$(target).attr("disabled",false);
        $(target).html(text);
    }
    
    clearCookie("ticking");
    sessionStorage.removeItem("tick_text");
    sessionStorage.removeItem("tick_target");
    sessionStorage.removeItem("tick_");
}

auto_reflash();
///////////////////倒计时  -end /////////////////////////